const app = getApp();
var config = app.globalData.config;
Page({

	data: {
		checked: false,
		wx_login_step: 0,
		agreement_show: false
	},

	onLoad(options) {

	},


	//获取基本信息
	getUserProfile(e) {
		if (this.data.checked == false) {
			wx.showToast({
				title: '请同意用户协议',
				icon: 'none',
				duration: 1000,
				mask: true
			});
			return;
		}
		wx.getUserProfile({
			desc: '用于完善会员资料',
			success: (res) => {
				this.setData({
					userInfo: res.userInfo
				})
				this.doLogin(res.userInfo);
			}
		})
	},

	//登录
	doLogin: function(userInfo) {
		var that = this;
		wx.login({
			success: function(res) {
				//获取登录的临时凭证
				//调用后端，获取微信的session_key,secret
				wx.request({
					url: config.wx_login,
					method: "post",
					header: {
						"Content-Type": "application/json",
					},
					data: {
						code: res.code,
						rawData: userInfo,
						versions: config.versions
					},
					success: function(result) {
						that.setData({
							wxapp_info: result.data.data,
							wx_login_step: 1
						})
					}
				})
			}
		})
	},


	//获取手机号码
	getPhoneNumber(e) { //授权手机号登录（获得授权后加密的串）
		var that = this;

		wx.checkSession({
			success(res) {
				if (e.detail.errMsg == 'getPhoneNumber:fail user deny') { //拒绝授权登录
					wx.showToast({
						title: '请授权绑定手机号',
						icon: 'none',
						duration: 1000,
						mask: true
					})
				} else {

					// 获取手机号
					wx.request({
						url: config.wx_phone,
						method: 'POST',
						header: {
							"Content-Type": "application/json",
						},
						data: {
							encryptedData: e.detail.encryptedData,
							iv: e.detail.iv,
							errMsg: e.detail.errMsg,
							session_key: that.data.wxapp_info.session_key,
							openid: that.data.wxapp_info.openid,
							unionid: that.data.wxapp_info.unionid,
							nickName: that.data.userInfo.nickName,
							avatarUrl: that.data.userInfo.avatarUrl,
							gender: that.data.userInfo.gender,
						},
						success: function(res) {
							if (res.data.code == 1) {
								wx.setStorageSync('userInfo', res.data.data.userInfo)
								app.globalData.userInfo = res.data.data.userInfo;
								wx.setStorageSync('token', res.data.data.token)
								app.globalData.token = res.data.data.token;
                
                wx.reLaunch({
                  url: '/pages/login/checkrole',
                })
                
								// var checkRole = res.data.data.userInfo.group_id;
								// if(checkRole==0 || checkRole=='0'){
								// 	wx.reLaunch({
								// 		url: '../index/index',
								// 	})
								// }else{
								// 	wx.reLaunch({
								// 		url: '../../teacher/index/tindex',
								// 	})
								// }
							} else {
								wx.showToast({
									title: res.data.msg,
									icon: 'none',
									duration: 1000,
									mask: true
								})
							}
						},
						fail: function() {
							wx.showToast({
								title: '获取授权信息失败',
								icon: 'none',
								duration: 1000,
								mask: true
							})
						}
					})
				}
			},
			fail() {
				that.showModal('获取授权信息失败')
			}
		})
	},

	//勾选用户协议
	protocol_check(e) {
		let _check = e.detail.value;
		if (_check.length > 0) {
			this.setData({
				checked: true
			})
		} else {
			this.setData({
				checked: false
			})
		}
	},

	//用户协议
	agreement_pop_open(e) {
		this.setData({
			agreement_show: true
		})
	},

	//关闭协议弹窗
	agreement_pop_close(e) {
		this.setData({
			agreement_show: false
		})
	}
})
